Set Variables
The Set Variables section allows you to put information into context by setting entities or local variables to specific values or to the result of specific functions.
To access the flow step Set Variables, in the Flow Step editor, click on the SetVariable section header. The section expands.
To set an entity or variable, click the Add New button. An empty line for variable appears and below it as many empty lines for the values as bot languages are (the first one is the bot default language).
Provide an entity or variable and in the key / value field set the desired value per language.
Set as many variables as you need on the flow step then save the step.
In DRUID 7.8 and higher, you can use two-stage set variables (both scalar and entity), eliminating the need to add two separate set variables to fulfill this requirement. For instance, instead of incorporating two set variables:
[[DrivingLicense]].ExpirationDate = '2024-05-05'
[[Employee]].DrivingLicense = [[DrivingLicense]]
You can now streamline the process with just one scalar set variable:
[[Employee]].DrivingLicense.ExpirationDate = '2024-05-05'
Similarly, for entity set variables, such as:
[[Employee]].Document.DrivingLicense = [[DrivingLicense]]
Set variables and multilingual entity fields
For DRUID 1.74 and higher, you can put information into context by setting entities to language specific values.
Prerequisite
- The entity field has the Holds multilanguage records option enabled. For more information, see Managing Entity Fields and Multilingual Entity Fields.
In the Set Variable section on the desired flow step, provide the desired entity as key and the multilingual entity field as key and as value for all languages. The multilingual entity field acts as an alias for the language-specific field clones and DRUID will automatically set values based on the user's language. You can also provide as value the language-specific field clones (which are available in entity object explorer). See the figure below.
For more information, see Managing Entity Fields and NER and Multilanguage Bots.
Setting Up Variables Examples
Example | Description |
---|---|
[[FinancialProduct]].PrincipalAmount = 35000 | Sets to a number. |
[[FinancialProduct]].Notes = “to refinance another loan” | Sets to a string. |
[[ChatUser]].Language = “en-us” | Sets to a string. |
[[Form]].CurrentWelcomeTime = new Date(); | Sets the CurrentWelcomeTime field of type DateTime, to the current date. |
[[Form]].LastWelcomeTime = [[Form]].LastWelcomeTime == null ? [[Form]].CurrentWelcomeTime : [[Form]].LastWelcomeTime | Uses the conditional (ternary) operator. |
[[Form]].MinutesFromLastWelcome = Math.abs([[Form]].CurrentWelcomeTime - [[Form]].LastWelcomeTime) / 36000 | Uses a math function. |
[[SelectedProduct]] = [[ProductList]][0] | Uses entity collections. Sets the [[SelectedProduct]] entity (type Product) with the first element from the array [[ProductList]]. |
[[SelectedProduct]].TotalAmount = [[SelectedProduct]].UnitPrice * [[SelectedProduct]].Quantity | |
[[Invoice]].LinesCount = [[InvoiceLineList]].Count |
Set variables using advanced coding
You can also set variables on flow steps using advanced coding. To do so, in the Set Variables section of your flow step, select the desired DRUID entity and click the edit icon.
The Code Editor page appears.
The Code Editor provides you with a set of predefined code template to help you define your JavaScript code. These templates are explained in detail below.
To add a template, click the Add default template button ( ) and in the code editor select the template that best suits your needs.
In the Code Editor, entities from the conversation context are referenced with the same syntax you are already familiar, using brackets. Example: [[ChatUser]], [[Account], etc.
Start from scratch using an empty inline function
You structure your code inside an inline function.
Set Entity Template
Use this template if you want to create an entity or if you want to set scalar attributes. Remove the unnecessary code to accommodate your needs.
Set entity template - Example
/*
Create an Entity: EntityFactory.CreateEntityByName("your_entity_name");
# Template Name: Set an Entity
function returning an Entity
*/
(
function ()
{
let newEntity = EntityFactory.CreateEntityByName("your_entity_name");
newEntity.Name = i.toString();
newEntity.Surname = i.toString();
newEntity.Status = "Draft";
/* You can refer any context slot. Example: [[ChatActivity]].Field1
You can read or set on the conversation context.
[[ChatActivity]].Field1 = "lore ipsum"
[[ChatActivity]].Amount = [[ChatActivity]].Amount * 1.5
*/
return newEntity;
}
)()
Set Entity Collection Template
Use this template if you want to create an entity collection. Remove the unnecessary code to accommodate your needs.
Entity Collection Template - Example
/*
Create an Entity Collection: EntityFactory.CreateCollection("<Entity Name>");
Create an Entity: EntityFactory.CreateEntityByName("<Entity Name>");
# Template Name: Set an Entity Collection
function returning an Entity Collection
Sample code for set variable: [[Account]].Tasks = <the code bellow>
*/
(
function ()
{
let vbTaskList = EntityFactory.CreateCollection("Task");
for (let i = 0; i < 200; i++)
{
let newTask = EntityFactory.CreateEntityByName("Task");
newTask.Nume = i.toString();
newTask.Prenume = i.toString();
newTask.Status = "Draft";
vbTaskList.Add(newTask);
}
return vbTaskList;
}
)();
Extensive methods for collections
You can use the following methods to work with collections:
Method | Example |
---|---|
Create a new collection |
Copy
|
Add new item at the end of the collection |
Copy
|
Insert new item to collection at the specified position |
Copy
Example - Add contract #20000 at position 2
|
Remove an item from a collection from the specified position | |
Get the number of elements in a collection | |
Refer an item inside a collection |